<?php
class Pageley_Gateway_Database_Condition_SingleSqlDecorator extends Pageley_Gateway_Database_Condition_AbstractSqlDecorator {

	private $query;

	public function __construct($decoratedCondition, $query) {
		parent::__construct($decoratedCondition);
		$this->query = $query;
	}

	protected function createSql() {
		$propertyName = $this->getDecoratedCondition()->getPropertyName();
		$type = $this->getQuery()->getType();
		$sqlKey = Pageley_Util_String::addUnderscores(Pageley_Util_String::replace($this->getDecoratedCondition()->getKey(), ".", "_"));
		return $this->getQuery()->getAlias($type) .
			".\"" . $this->getColumnName($propertyName) .
			"\" " . $this->getOperator() .
			" :" . Pageley_Util_String::addUnderscores($sqlKey);
	}

	public function getValues() {
		$values = $this->getDecoratedCondition()->getValues();
        $sqlValues = new Pageley_Util_ArrayMap();
		$type = $this->getQuery()->getType();
		$propertyName = $this->getDecoratedCondition()->getPropertyName();
		$factory = Pageley_Serializer_Converter_Factory::getInstance();
		$converter = $factory->getConverter($type->getAttribute($propertyName));
		foreach($values as $key => $value) {
			$sqlKey = Pageley_Util_String::addUnderscores(Pageley_Util_String::replace($key, ".", "_"));
            $sqlValues->put($sqlKey, $converter->convertIntoFormat($value));
        }
        return $sqlValues;
	}

	public function getQuery() {
		return $this->query;
	}

	protected function getColumnName($propertyName) {
		return $this->query->getColumnName($propertyName);
    }
}